Use case: COMP window mode comparing by interruption ¶
Description of usage ¶
|
Operation |
HAL1 |
HAL2 |
|---|---|---|
|
Start comparing |
Start two comparators in window mode using
|
Link the two comparators for window mode using
|
|
Comparator output trigger callback |
Register the callback using HAL_COMP_RegisterCallback(), or implement HAL_COMP_TriggerCallback(). |
Register the callback using HAL_COMP_RegisterOutputTriggerCallback(), or implement HAL_COMP_OutputTriggerCallback(). |
|
Retrieve comparing result |
Retrieve comparing results using
|
Retrieve comparing results using
|
|
Loop for multiple iterations |
Possible multiple iterations if the comparator output has changed since the last read. |
Possible multiple iterations if the comparator output has changed since the last read. |
|
Stop comparing |
Stop comparator analog features using
|
Stop comparator analog features using
Note Since the two comparators are linked, after calling this function both comparators will be stopped. |
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
int main(void)
{
/*initialize and configure your comparing input*/
/* Start the COMP */
HAL_COMP_Start_IT(&comp1);
HAL_COMP_Start_IT(&comp2);
while(HAL_COMP_GetOutputLevel(hcomp) != COMP_OUTPUT_LEVEL_HIGH)
{
/* generate voltage on comparator inputs */
/* add a Wait time for voltage stabilization*/
}
/* Stop the COMP */
HAL_COMP_Stop_IT(&comp1);
HAL_COMP_Stop_IT(&comp2);
}
void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
{
if ((hcomp->Instance == COMP1) || (hcomp->Instance == COMP2))
{
/* Handle the comparator event here */
if (HAL_COMP_GetOutputLevel(hcomp) == COMP_OUTPUT_LEVEL_HIGH)
{
/* Comparator output is high */
}
else
{
/* Comparator output is low */
}
}
}
|
int main(void)
{
/*initialize the comparator*/
/*link the two comparators*/
HAL_COMP_WINDOW_SetHandle(&hcomp1, &hcomp2);
/*Configure your comparing input*/
/* Start the COMP */
HAL_COMP_WINDOW_Start_IT(&comp1);
while(HAL_COMP_WINDOW_GetOutputLevel(hcomp) != HAL_COMP_OUTPUT_LEVEL_HIGH)
{
/* generate voltage on comparator inputs */
/* add a Wait time for voltage stabilization*/
}
/* Stop the COMP */
HAL_COMP_WINDOW_Stop_IT(&comp1);
}
void HAL_COMP_OutputTriggerCallback(COMP_HandleTypeDef *hcomp)
{
/* Handle the comparator event here */
if (HAL_COMP_WINDOW_GetOutputLevel(hcomp) == HAL_COMP_OUTPUT_LEVEL_HIGH)
{
/* Comparator output is high */
}
else
{
/* Comparator output is low */
}
}
|
Key changes ¶
Rename the trigger callback.
Rework COMP window mode.
Related specific concept: rework COMP Window mode.
![@startuml
title: COMP window mode operation using programming model: interruption
participant "User Application\n(thread mode)" as appli
participant "User Application\n(handler mode)" as appli_IRQ
participant "<font color=green><b>HAL COMP</b></font>" as COMP
participant "HAL EXTI" as EXTI
participant "System HW interruption" as interruption
group#Bisque #Linen COMP operation
appli->COMP : HAL_COMP_Start_IT(COMP1)
COMP->COMP : Enable selected interruptions\nActivate comparator analog features
COMP->appli
appli->COMP : HAL_COMP_Start_IT(COMP2)
COMP->COMP : Enable selected interruptions\nActivate comparator analog features
COMP->appli
loop#lightgrey Possible multiple iterations
interruption <[#red]-x? : <font color=red><b>COMP IRQ event
interruption -> appli_IRQ : COMPx_IRQHandler()
activate appli_IRQ #DarkSalmon
appli_IRQ -> COMP : HAL_COMP_IRQHandler()
COMP -> EXTI : Manage EXTI line flag
COMP <- EXTI
COMP -> appli_IRQ: HAL_COMP_OutputTriggerCallback()
appli_IRQ->COMP : HAL_COMP_GetOutputLevel(COMP1)
COMP --> appli_IRQ: Level below/within/above
appli_IRQ->COMP : HAL_COMP_GetOutputLevel(COMP2)
COMP --> appli_IRQ: Level below/within/above
appli_IRQ -> interruption : (return from ISR)
deactivate appli_IRQ
end
appli->COMP : HAL_COMP_Stop_IT(COMP1)
return
appli->COMP : HAL_COMP_Stop_IT(COMP2)
return
end
@enduml](../../../../../_images/plantuml-50c013e0e909ba5fbf3e571fdb69d82298b0b7a9.png)
![@startuml
title: COMP window mode operation using programming model: interruption
participant "User Application\n(thread mode)" as appli
participant "User Application\n(handler mode)" as appli_IRQ
participant "<font color=green><b>HAL COMP</b></font>" as COMP
participant "HAL EXTI" as EXTI
participant "System HW interruption" as interruption
group#Bisque #Linen COMP operation
appli->COMP : HAL_COMP_WINDOW_Start_IT()
COMP->COMP : Enable selected interruptions\nActivate comparator analog features
COMP->appli
loop#lightgrey Possible multiple iterations
interruption <[#red]-x? : <font color=red><b>COMP IRQ event
interruption -> appli_IRQ : COMPx_IRQHandler()
activate appli_IRQ #DarkSalmon
appli_IRQ -> COMP : HAL_COMP_IRQHandler()
COMP -> EXTI : Manage EXTI line flag
COMP <- EXTI
COMP -> appli_IRQ: HAL_COMP_OutputTriggerCallback()
appli_IRQ->COMP : HAL_COMP_WINDOW_GetOutputLevel()
COMP --> appli_IRQ: Level below/within/above
appli_IRQ -> interruption : (return from ISR)
deactivate appli_IRQ
end
appli->COMP : HAL_COMP_WINDOW_Stop_IT()
return
end
@enduml](../../../../../_images/plantuml-e8b43d42a6bbc99345b6ff1094cb63c908f37573.png)